package de.flojo.game;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.MouseInfo;
import java.awt.PointerInfo;
import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Vector;

public class Ball
{
	public static final double GRAVITY = 1.5;
	
	public double x, y;
	public double width, height;
	public double x2, y2;
	public Color color = new Color(255, 0, 0);
	
	public double velocity = 0;
	public double direction;
	
	public ArrayList<Force> forces;
	
	public int collisionCounter = 0;
	
	public Ball(double x, double y, double width, double height)
	{
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;
		x2 = x + width;
		y2 = y + height;
		
		forces = new ArrayList<Force>();
		
		direction = 0;
	}
	
	public void update(ArrayList<Rectangle> colliders)
	{
		color = new Color(255, 0, 0);
		
		Force grav = new Force();
		grav.force = GRAVITY;
		grav.direction = Math.PI * 1.5;
		forces.add(grav);
		
		double tempDirections = 0;
		double tempForces = 0;
		
		Iterator<Force> fIter = forces.iterator();
		while(fIter.hasNext())
		{
			Force f = fIter.next();
			x += Math.sin(f.direction + (Math.PI / 2)) * f.force;
			y += Math.cos(f.direction + (Math.PI / 2)) * f.force;
			
			tempDirections += f.direction * f.force;
			tempForces += f.force;
			
			f.update();
			if(f.force < Force.min) fIter.remove();
		}
		
		direction = tempDirections / tempForces;
	}
	
	public void draw(Graphics g)
	{
		g.setColor(color);
		g.fillOval((int)x, (int)y, (int)width, (int)height);
	}
	
	public Ellipse2D.Double toEllipse2D()
	{
		return new Ellipse2D.Double(x, y, width, height);
	}
}
